package top.glike.ssl.auto.domain.other;

import java.util.Date;

/**
 * SSL证书信息实体类
 */
public class SSLCertificate {

    /**
     * 证书ID
     */
    private Long certificateId;

    /**
     * 证书名称
     */
    private String certificateName;

    /**
     * 域名
     */
    private String domain;

    /**
     * 证书状态
     */
    private String status;

    /**
     * 证书类型
     */
    private String certificateType;

    /**
     * 签发时间
     */
    private Date issueDate;

    /**
     * 到期时间
     */
    private Date expireDate;

    /**
     * 是否即将到期
     */
    private boolean nearExpiry;

    /**
     * 距离到期天数
     */
    private long daysToExpire;

    /**
     * 检查证书是否即将到期
     */
    public boolean checkNearExpiry(int checkDays) {
        if (expireDate == null) {
            return false;
        }

        long currentTime = System.currentTimeMillis();
        long expireTime = expireDate.getTime();
        long daysDiff = (expireTime - currentTime) / (24 * 60 * 60 * 1000);

        this.daysToExpire = daysDiff;
        this.nearExpiry = daysDiff <= checkDays && daysDiff >= 0;

        return this.nearExpiry;
    }

    /**
     * 检查证书是否已过期
     */
    public boolean isExpired() {
        if (expireDate == null) {
            return false;
        }
        boolean expired = new Date().after(expireDate);

        // 如果已过期，重新计算过期天数（负数表示已过期多少天）
        if (expired) {
            long currentTime = System.currentTimeMillis();
            long expireTime = expireDate.getTime();
            this.daysToExpire = (expireTime - currentTime) / (24 * 60 * 60 * 1000);
        }

        return expired;
    }

    /**
     * 获取过期状态描述
     */
    public String getExpiryStatusDescription() {
        if (expireDate == null) {
            return "未知到期时间";
        }

        // 重新计算天数以确保准确性
        long currentTime = System.currentTimeMillis();
        long expireTime = expireDate.getTime();
        long daysDiff = (expireTime - currentTime) / (24 * 60 * 60 * 1000);

        if (daysDiff < 0) {
            return "已过期" + Math.abs(daysDiff) + "天";
        } else {
            return "还剩" + daysDiff + "天";
        }
    }

    @Override
    public String toString() {
        return String.format("SSL证书[ID=%d, 域名=%s, 状态=%s, 到期时间=%s, %s]",
                certificateId, domain, status, expireDate, getExpiryStatusDescription());
    }

    // Getter and Setter methods
    public Long getCertificateId() {
        return certificateId;
    }

    public void setCertificateId(Long certificateId) {
        this.certificateId = certificateId;
    }

    public String getCertificateName() {
        return certificateName;
    }

    public void setCertificateName(String certificateName) {
        this.certificateName = certificateName;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCertificateType() {
        return certificateType;
    }

    public void setCertificateType(String certificateType) {
        this.certificateType = certificateType;
    }

    public Date getIssueDate() {
        return issueDate;
    }

    public void setIssueDate(Date issueDate) {
        this.issueDate = issueDate;
    }

    public Date getExpireDate() {
        return expireDate;
    }

    public void setExpireDate(Date expireDate) {
        this.expireDate = expireDate;
    }

    public boolean isNearExpiry() {
        return nearExpiry;
    }

    public void setNearExpiry(boolean nearExpiry) {
        this.nearExpiry = nearExpiry;
    }

    public long getDaysToExpire() {
        return daysToExpire;
    }

    public void setDaysToExpire(long daysToExpire) {
        this.daysToExpire = daysToExpire;
    }
}